Imports System.ComponentModel
Imports System.Web.Script.Serialization

<ToolboxData("<{0}:ListBox runat=server></{0}:ListBox>")> _
<ParseChildren(True, "Items")> _
<PersistChildren(False)> _
Public Class ListBox
    Inherits WebControls.ListBox
    Implements IJObjective

    Private m_sNamespace As String
    Private m_sPropertyName As String
    Private m_sConstructor As String
    Private m_sDataID As String

    Public Enum Specify
        <Description("-- Please Specify --")> _
        PleaseSpecify = -1
    End Enum

    Public Class ListItem
        Public Text As String = String.Empty
        Public Value As String = String.Empty
        Public Selected As Boolean = False
    End Class

    Public Class ClientObject
        Public Options() As ListItem

        <ScriptIgnore()> _
        Public Property Selected() As ListItem
            Get
                For Each objItem As ListItem In Options
                    If objItem.Selected Then
                        Return objItem
                    End If
                Next
                Return Nothing
            End Get
            Set(ByVal value As ListItem)
                For Each objItem As ListItem In Options
                    If objItem.Value = value.Value Then
                        objItem.Selected = True
                    End If
                Next
            End Set
        End Property
    End Class

    Public Property [Namespace]() As String Implements IJObjective.Namespace
        Get
            Return m_sNamespace
        End Get
        Set(ByVal value As String)
            m_sNamespace = value
        End Set
    End Property

    Public Property ObjectName() As String Implements IJObjective.ObjectName
        Get
            Return m_sPropertyName
        End Get
        Set(ByVal value As String)
            m_sPropertyName = value
        End Set
    End Property

    Public Property Constructor() As String Implements IJObjective.Constructor
        Get
            Return m_sConstructor
        End Get
        Set(ByVal value As String)
            m_sConstructor = value
        End Set
    End Property

    Public Property DataID() As String Implements IJObjective.DataID
        Get
            Return m_sDataID
        End Get
        Set(ByVal value As String)
            m_sDataID = value
        End Set
    End Property

    Public Overrides ReadOnly Property ClientID() As String
        Get
            Return ID
        End Get
    End Property

    Public Sub New()
        DataID = Common.GetUniqueID(Context)
    End Sub

    Private Sub ListBox_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        EnsureChildControls()

        If String.IsNullOrEmpty(Constructor) Then
            If Me.SelectionMode = WebControls.ListSelectionMode.Single Then
                Constructor = "new jObjective.Select($('#" & DataID & "'))"
            Else
                'TODO: Use Select Multiple
                Constructor = "new jObjective.Select($('#" & DataID & "'))"
            End If
        End If
    End Sub

    Private Sub ListBox_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ID = DataID
    End Sub
End Class